package com.example.demo.controller;

import com.example.demo.constant.Constant;
import com.example.demo.enums.BookStatusEnum;
import com.example.demo.enums.ResultCodeEnum;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.*;
import com.example.demo.service.BookService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/book")
@Component
public class BookController {

    //获取图书列表
    @Autowired   //从spring当中获得对象
    private BookService bookService;
//    @RequestMapping("/getList")
//    public List<BookInfo> getList(){
//        //BookService bookService=new BookService();
//        return bookService.getList();
//    }

    //添加图书
    @RequestMapping(value = "/addBook",produces = "application/json")  //如果返回值为String 经过统一结果适配,返回值会变成String,导致与前端交互出错
    public String addBook(BookInfo bookInfo){
        log.info("添加图书, request:{}"+bookInfo);
        //1.参数校验
        if (!StringUtils.hasLength(bookInfo.getBookName())
                ||!StringUtils.hasLength(bookInfo.getAuthor())
                ||bookInfo.getCount()==null
                ||bookInfo.getPrice()==null
                ||!StringUtils.hasLength(bookInfo.getPublish())
                ||bookInfo.getStatus()==null){
            log.error("添加图书,参数不合法,request {}",bookInfo);
            return "参数不合法";
        }

        //2.存储数据
        try{
            bookService.addBook(bookInfo);
            //3.返回结果
            return "";
        }catch (Exception e){
            log.error("添加图书异常,e:"+e);
            return "添加图书发生异常";
        }
    }

    @RequestMapping("/getListByPage")
    public Result<ResponseResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
        //参数校验
        //返回数据
//        if(session.getAttribute(Constant.SESSION_USER_KEY)==null){
//            return Result.unlogin();
//        }
//        UserInfo userInfo=(UserInfo) session.getAttribute(Constant.SESSION_USER_KEY);
//        if(userInfo==null||userInfo.getId()<=0){
//            //用户未登录
//            return Result.unlogin();
//        }

        ResponseResult<BookInfo> listByPage=bookService.getListByPage(pageRequest);
        return Result.success(listByPage);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("查询图书信息,bookId"+bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBook")
    public Result updateBook(BookInfo bookInfo){
        log.info("修改图书,bookInfo:{}",bookInfo);
        try{
            bookService.updateBook(bookInfo);
            return Result.success("");
        }catch (Exception e){
            log.error("修改图书发生异常,e",e);
            return Result.fail("修改图书发生异常");
        }
    }



    @RequestMapping(value = "/deleteBook")
    public Result deleteBook(Integer bookId){
        log.info("删除图书, bookId: {}", bookId);
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            bookService.updateBook(bookInfo);
            //成功
            return Result.success("");
        }catch (Exception e){
            log.error("删除图书发生异常, e: ", e);
            return Result.fail("删除图书发生异常");
        }
    }

    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书,ids:{}",ids);
        try{
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败,e",e);
            return false;
        }
    }
}
